package com.qzdsoft.erpcloud.service.stock.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.stock.StockInbound;
import com.qzdsoft.erpcloud.mapper.stock.StockInboundMapper;
import com.qzdsoft.erpcloud.mapper.stock.StockInboundQueryMapper;
import com.qzdsoft.erpcloud.service.stock.StockInboundService;
import com.qzdsoft.erpcloud.vo.stock.StockInboundInfo;
import com.qzdsoft.erpcloud.vo.stock.StockInboundQueryInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.pu.StockInboundOrderStatus;
@Service
public class StockInboundServiceImpl implements StockInboundService {
	@Autowired
	private StockInboundQueryMapper stockInboundQueryMapper;
	@Autowired
	private StockInboundMapper stockInboundMapper;
	
	@Override
	public LayTableResponse<StockInboundInfo> findStockInboundOrdersBy(StockInboundQueryInfo info) {
		PageHelper.startPage(info.getPage(), info.getLimit());
		List<StockInboundInfo> stockInboundInfo = stockInboundQueryMapper.selectAll(info);
		LayTableResponse<StockInboundInfo> si = new LayTableResponse<StockInboundInfo>(stockInboundInfo);
		return si;
	}
    @Override
    public StockInboundInfo findStockInboundOrderBy(Integer id)
    {
        return stockInboundQueryMapper.selectOneBy(id);
    }
    @Override
    public Response<String> edit(StockInbound info)
    {
    	StockInbound stockInboud = this.stockInboundMapper.selectByPrimaryKey(info.getId());
    	//设置修改时间
    	stockInboud.setModifyTime(new Date());
    	//设置修改人
    	stockInboud.setModifyId(1);
    	//供应商id
    	stockInboud.setSupplyId(info.getSupplyId());
    	//供应商联系人id
    	stockInboud.setSupplyContactId(info.getSupplyContactId());
    	//结算方式id
    	stockInboud.setStatementId(info.getStatementId());
    	//计算方式名称
    	stockInboud.setStatementName(info.getStatementName());
    	//结算天数
    	stockInboud.setStatementDays(info.getStatementDays());
    	//结算币种id
    	stockInboud.setStatementCurrencyId(info.getStatementCurrencyId());
    	//结算税率
    	stockInboud.setStatementTaxRate(info.getStatementTaxRate());
    	//结算汇率
    	stockInboud.setStatementExchangeRate(info.getStatementExchangeRate());
    	//备注
    	stockInboud.setRemark(info.getRemark());
    	this.stockInboundMapper.updateByPrimaryKey(stockInboud);
//    	StockInboundInfo data = stockInboundQueryMapper.selectOneBy(id);
//    	Response<String> result = new Response<StockInboundInfo>(200,"单据【"+data.getOrderNo()+"】表头修改成功！！！",data);
    	return Response.success("单据修改成功");
    }
	@Override
	@Transactional
	public Response<String> save(StockInbound info) {
		//入库类别 默认采购入库
//		info.setInboundType(0);
		//创建时间
		info.setCtime(new Date());
		//创建人
		info.setCreatorId(1);
//		info.setModifyId(1);
//		info.setModifyTime(new Date());
		//审核状态 默认未审核
		info.setReviewStatus(Integer.parseInt(StockInboundOrderStatus.NOT_REVIEW_CODE));
		stockInboundMapper.insert(info);
		return Response.success("单据单新增成功！！");
	}
	@Override
	public Response<String> del(Integer id) {
		stockInboundMapper.deleteByPrimaryKey(id);
		return Response.success("单据据删除成功！！");
	}
	@Override
	public Response<String> review(Integer id) {
		StockInbound stockInboud = this.stockInboundMapper.selectByPrimaryKey(id);
		if(stockInboud.getReviewStatus()==Integer.parseInt(StockInboundOrderStatus.NOT_REVIEW_CODE)) {
			//状态
			stockInboud.setReviewStatus(Integer.parseInt(StockInboundOrderStatus.HAS_REVIEW_CODE));
			//修改人
			stockInboud.setModifyId(1);
			//修改时间
			stockInboud.setModifyTime(new Date());
			//审核人
			stockInboud.setReviewId(1);
			//审核时间
			stockInboud.setReviewTime(new Date());
			this.stockInboundMapper.updateByPrimaryKeySelective(stockInboud);
			return Response.success("单据审核成功！！");
		}else{
			return Response.error("该单据不是未审核状态，不能审核操作！");
		}
		
	}
	@Override
	public Response<String> celview(Integer id) {
		StockInbound stockInboud = this.stockInboundMapper.selectByPrimaryKey(id);
		if(stockInboud.getReviewStatus()==Integer.parseInt(StockInboundOrderStatus.HAS_REVIEW_CODE)) {
			//状态 StockInboundOrderStatus
//			stockInboud.setReviewStatus(AuditState.NOT_REVIEW_CODE.getStatenum());
			stockInboud.setReviewStatus(Integer.parseInt(StockInboundOrderStatus.NOT_REVIEW_CODE));
			//修改人
			stockInboud.setModifyId(1);
			//修改事件
			stockInboud.setModifyTime(new Date());
			stockInboud.setReviewId(null);
			stockInboud.setReviewTime(null);
			this.stockInboundMapper.updateByPrimaryKeySelective(stockInboud);
			return Response.success("单据弃审成功！！");
		}else{
			return Response.success("该单据不是已审核状态，不能进行弃审操作！");
		}
	}
	@Override
	public Response<String> close(Integer id) {
		StockInbound stockInboud = this.stockInboundMapper.selectByPrimaryKey(id);
		if(stockInboud.getReviewStatus()!=Integer.parseInt(StockInboundOrderStatus.CLOSE_CODE)) {
			stockInboud.setReviewStatus(Integer.parseInt(StockInboundOrderStatus.CLOSE_CODE));
			//修改人
			stockInboud.setModifyId(1);
			//修改事件
			stockInboud.setModifyTime(new Date());
			this.stockInboundMapper.updateByPrimaryKeySelective(stockInboud);
			return Response.success("单据关闭成功！！");
		}else{
			return Response.success("该单据已关闭，不能进行关闭操作！");
		}
	}
	
	@Override
	public Response selectBySourceId(Integer id) {
		List<Map<String,Object>> result = stockInboundQueryMapper.selectBySourceId(id);
		if(result.size()>0) {
			return new Response(ResultEnum.SUCCESS,result.get(0));
		}else{
			return Response.error("未找到该来源单据号信息！");
		}
		
	}
	@Override
	public Response selectByOutOrderId(Integer id) {
		List<Map<String,Object>> result = stockInboundQueryMapper.selectByOutOrderId(id);
		if(result.size()>0) {
			return new Response(ResultEnum.SUCCESS,result.get(0));
		}else{
			return Response.error("未找到该来源单据号信息！");
		}
	}
	
	
}
